#include<bits/stdc++.h>
using namespace std;
int Tc;
int a[200005],n;
typedef long long ll;
ll f[200005],Mx,mx[1000005];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&Tc);
    while(Tc--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        ll ans=0;
        for(int i=1;i<n;i++)if(a[i]==a[i+1])ans+=a[i];
        n=unique(a+1,a+n+1)-a-1;
        Mx=0;
        for(int i=0;i<=1000000;i++)mx[i]=-0x3f3f3f3f3f3f3f3f;
        for(int i=1;i<=n;i++){
            f[i]=max(Mx,mx[a[i]]+a[i]);
            Mx=max(Mx,f[i]);
            mx[a[i-1]]=max(mx[a[i-1]],f[i]);
            // printf("f[%d]=%lld\n",i,f[i]);
        }
        printf("%lld\n",Mx+ans);
    }
    return 0;
}